import React from 'react';
import { connect } from 'dva';
import { Link, browserHistory } from 'dva/router';
import { Table, Row, Col, Form, Input, Radio, Select, Button, message, Popconfirm } from 'antd';
const R = require('ramda');
import RightBox from '../../../components/commons/RightBox';
import RightConstants from '../../../utils/RightConstants';
import Constants from '../../../utils/Constants';
import BizParams from '../../../components/commons/BizParams';
import VipCustomerDetail from '../../../components/vip/VipCustomerDetail';
import VipSendTaxInfo from '../../../components/vip/VipSendTaxInfo';
const FormItem = Form.Item;
const Option = Select.Option;
const RadioGroup = Radio.Group;
const TRANSFER = ["未转移", "已转移"];
const IE_AUTH = ["没有", "有"];
const INVOICE_STATUS = ["未邮寄", "已邮寄", "不需要"];
export default connect(({ vipManage }) => ({ vipManage }))(
    Form.create()(class vipManage extends React.Component {
        constructor(props) {
            super(props);
        }

        componentDidMount() {
            this.props.dispatch({ type: 'vipManage/queryRegCustomers' });//vip会员客户查询
            this.props.dispatch({ type: 'vipManage/queryGoods' });//查询产品列表
        }

        collectSearchCondition = () => {
            let conditions = this.props.form.getFieldsValue();
            conditions.page = this.props.vipManage.page;
            return conditions;
        }

        handleView = (record) => {
            this.props.dispatch({ type: 'vipManage/getRegCustomerById', payload: { id: record.id } });//获取会员客户详情
            this.vipCustomerDetail.handleOpen(true, true, this.collectSearchCondition(), record);
        }

        handleDelete = (record) => {
            this.props.dispatch({ type: 'vipManage/deleteRegCustomerById', payload: { id: record.id, searchConditions: this.collectSearchCondition() } });//删除会员客户
        }

        handleTransfer = (record) => {
            this.props.dispatch({ type: 'vipManage/transferRegCustomerById', payload: { id: record.id, searchConditions: this.collectSearchCondition() } });//转移会员客户
        }

        handleTaxInfoSend = (record) => {
            this.vipSendTaxInfo.handleOpen(this.collectSearchCondition(), record);
        }

        handleSearch = () => {
            let values = this.props.form.getFieldsValue();
            let newPage = this.props.vipManage.page;
            newPage.page = 1;
            this.props.dispatch({
                type: 'vipManage/queryRegCustomers',
                payload: {
                    cstName: values.cstName,
                    cstType: values.cstType,
                    vipType: values.vipType,
                    status: values.status,
                    page: newPage
                }
            });
        };

        handleExport = () => {
            let values = this.props.form.getFieldsValue();
            this.props.dispatch({
                type: 'vipManage/exportRegCustomers',
                payload: {
                    cstName: values.cstName,
                    cstType: values.cstType,
                    vipType: values.vipType,
                    status: values.status
                }
            });
        };

        columns = [
            { title: '客户名称', dataIndex: 'cstName', key: 'cstName' },
            { title: '客户类型', dataIndex: 'cstTypeValue', key: 'cstTypeValue' },
            { title: '会员类型', dataIndex: 'vipTypeValue', key: 'vipTypeValue' },
            {
                title: '转移状态', dataIndex: 'transfer', key: 'transfer',
                render: (text, record, index) => {
                    return (TRANSFER[record.transfer])
                }
            },
            {
                title: '进出口权', dataIndex: 'ieRights', key: 'ieRights',
                render: (text, record, index) => {
                    return (IE_AUTH[record.ieRights])
                }
            },
            {
                title: '发票状态', dataIndex: 'invoiceStatus', key: 'invoiceStatus',
                render: (text, record, index) => {
                    return (R.isNil(INVOICE_STATUS[record.invoiceStatus]) ? INVOICE_STATUS[2] : INVOICE_STATUS[record.invoiceStatus])
                }
            },
            { title: '业务经理', dataIndex: 'waiterName', key: 'waiterName', },
            {
                title: '客户状态', dataIndex: 'status', key: 'status',
                render: (text, record, index) => {
                    return (Constants.VIP_CUSTOMER_STATUS[record.status])
                }
            },
            {
                title: '操作', key: 'oprater',
                render: (text, record, index) => {
                    let deleteLink = <Popconfirm onConfirm={this.handleDelete.bind(this, record)} okText="是" cancelText="否"
                        placement="leftTop" title={'数据删除后将不可以恢复，是否继续删除？'}>
                        <a href="#">删除</a>
                    </Popconfirm>
                    deleteLink = <RightBox resource={RightConstants.VIP.VIP_MANAGE_DELETE} split={true}>{deleteLink}</RightBox>;
                    let transferLink = <Popconfirm onConfirm={this.handleTransfer.bind(this, record)} okText="是" cancelText="否"
                        placement="leftTop" title={'该功能用于会员客户转移至客户管理模块以实现运营下单功能，只能转移一次，一旦转移无法恢复，请确认！'}>
                        <a href="#">转移</a>
                    </Popconfirm>
                    transferLink = record.transfer !== "1" && record.status === "3" ? <RightBox resource={RightConstants.VIP.VIP_MANAGE_TRANSFER} split={true}>{transferLink}</RightBox> : null;
                    let taxInfoSend = record.status === "3" ? <RightBox resource={RightConstants.VIP.VIP_MANAGE_TAX_INFO_SEND} split={true}><a href="#" onClick={this.handleTaxInfoSend.bind(this, record)}>发送财税附件</a></RightBox> : null;
                    let viewLink = <a href="#" onClick={this.handleView.bind(this, record)}>查看</a>
                    return (<span>{deleteLink}{transferLink}{taxInfoSend}{viewLink}</span>);
                }
            }
        ];

        render() {
            let dataSource = R.isNil(this.props.vipManage.list) ? {} : this.props.vipManage.list;
            let formItemLayout = { labelCol: { span: 6 }, wrapperCol: { span: 18 } };
            const { getFieldDecorator } = this.props.form;
            let values = this.props.form.getFieldsValue();
            let pagination = {
                showTotal: total => `共 ${total} 条数据`,
                current: this.props.vipManage.page.page,
                pageSize: this.props.vipManage.page.size,
                total: this.props.vipManage.page.totalRow,
                showSizeChanger: true,
                onShowSizeChange: (current, size) => {
                    let newPage = this.props.vipManage.page;
                    newPage.size = size;
                    newPage.page = 1;
                    this.props.dispatch({
                        type: 'vipManage/queryRegCustomers',
                        payload: {
                            cstName: values.cstName,
                            cstType: values.cstType,
                            vipType: values.vipType,
                            status: values.status,
                            page: newPage
                        }
                    });
                },
                onChange: (pageNumber, pageSize) => {
                    let newPage = this.props.vipManage.page;
                    newPage.page = pageNumber;
                    this.props.dispatch({
                        type: 'vipManage/queryRegCustomers',
                        payload: {
                            cstName: values.cstName,
                            cstType: values.cstType,
                            vipType: values.vipType,
                            status: values.status,
                            page: newPage
                        }
                    });
                }
            }
            let statusArray = [<Radio value="" key={"all"}>全部</Radio>];
            R.mapObjIndexed((value, key) => {
                if (key !== "0") statusArray.push(<Radio value={key} key={key}>{value}</Radio>);
            }, Constants.VIP_CUSTOMER_STATUS);
            return (
                <div>
                    <Row>
                        <Col span={6}><FormItem {...formItemLayout} label={'客户名称'}>{getFieldDecorator('cstName')(<Input />)}</FormItem></Col>
                        <Col span={6}><FormItem {...formItemLayout} label={'客户类型'}>
                            {getFieldDecorator('cstType')(
                                <BizParams sprCode={"cstType"} style={{ width: '100%' }} all={true} />
                            )}
                        </FormItem></Col>
                    </Row>
                    <Row>
                        <Col span={6}><FormItem {...formItemLayout} label={'会员类型'}>
                            {getFieldDecorator('vipType')(
                                <Select style={{ width: '100%' }} disabled={this.props.disabled} showSearch placeholder="可通过输入关键字搜索" filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}>
                                    <Select.Option value={""} key="all">全部</Select.Option>
                                    {
                                        !R.isNil(this.props.vipManage.goodsList) ? this.props.vipManage.goodsList.map(item => {
                                            return (<Select.Option value={item.id + ""} key={item.id + ""}>{item.name}</Select.Option>);
                                        }) : ''
                                    }
                                </Select>
                            )}
                        </FormItem></Col>
                    </Row>
                    <Row>
                        <Col span={6}>
                            <FormItem {...formItemLayout} label={'状态'}>
                                {getFieldDecorator('status', {
                                    initialValue: ""
                                })(
                                    <RadioGroup style={{ width: 800 }}>
                                        {statusArray}
                                    </RadioGroup>
                                    )}
                            </FormItem>
                        </Col>
                        <Col span={6} style={{ float: 'right' }}>
                            <Button type="primary" icon="search" onClick={this.handleSearch.bind(this)}>查询</Button>
                            <Button type="primary" style={{ marginLeft: 20 }} onClick={this.handleExport.bind(this)}>导出</Button>
                        </Col>
                    </Row>
                    <Table rowKey={record => record.id} dataSource={dataSource} columns={this.columns} bordered={true} size={'middle'} pagination={pagination} />
                    <VipCustomerDetail wrappedComponentRef={(inst) => this.vipCustomerDetail = inst} dataSource={this.props.vipManage.dataSource} dispatch={this.props.dispatch} goodsList={this.props.vipManage.goodsList}/>
                    <VipSendTaxInfo wrappedComponentRef={(inst) => this.vipSendTaxInfo = inst} dispatch={this.props.dispatch} />
                </div>
            )
        }
    })
)
